# 保存为 `sum_video_duration.ps1`
$folder = "D:\xxx"  # 替换为你的文件夹路径

$totalMilliseconds = 0
$videoExtensions = @("*.mp4", "*.mkv", "*.avi", "*.mov", "*.flv", "*.wmv")

# 递归获取所有视频文件
$videoFiles = Get-ChildItem -Path $folder -Recurse -Include $videoExtensions

# 遍历每个文件并提取时长
foreach ($file in $videoFiles) {
    $durationMs = mediainfo --Inform="General;%Duration%" $file.FullName
    if ($durationMs -match '^\d+$') {
        $totalMilliseconds += [double]$durationMs
        Write-Host "已处理: $($file.Name) → 时长 $([math]::Round($durationMs/1000)) 秒"
    } else {
        Write-Host "警告: 无法读取文件 $($file.Name)"
    }
}

# 计算总时长（毫秒 → 小时:分钟:秒）
$totalSeconds = $totalMilliseconds / 1000
$totalTime = [TimeSpan]::FromSeconds($totalSeconds)
$hours = $totalTime.Hours + $totalTime.Days * 24
$minutes = $totalTime.Minutes
$seconds = $totalTime.Seconds

Write-Host "`n总时长: ${hours}小时 ${minutes}分钟 ${seconds}秒"